package com.neu.flashmall.user.controller;

import com.neu.common.utils.Result;
import com.neu.flashmall.user.entity.UmsCollectSpu;
import com.neu.flashmall.user.service.UmsCollectSpuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/ums_collectSpu")
@Api(value = "收藏商品Controller")
public class UmsCollectSpuController {
    @Resource
    private UmsCollectSpuService umsCollectSpuService;

    @GetMapping("/getPaged")
    public Result getPaged(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "4") Integer pageSize,
                           @Param(value = "userId") Long userId){
        return Result.ok().put(umsCollectSpuService.getPaged(pageNum,pageSize,userId));
    }

    @GetMapping("/selectCollectSpuByUserId")
    @ApiOperation(value = "通过userId获得所有收藏Spu")
    public Result selectCollectSpuByUserId(@Param(value = "userId") Long userId){
        return Result.ok().put(umsCollectSpuService.selectCollectSpuByUserId(userId));
    }

    @PostMapping("/insert")
    @ApiOperation(value = "添加收藏商品")
    public Result insert(UmsCollectSpu umsCollectSpu){
        if(umsCollectSpuService.insert(umsCollectSpu) > 0){
            return Result.ok().setMessage("添加收藏成功");
        }
        return Result.error().setMessage("添加收藏失败");
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除收藏商品")
    public Result delete(Long collectSpuId){
        String result = umsCollectSpuService.delete(collectSpuId);
        if(result.equals("已取消收藏")){
            return Result.ok().setMessage(result);
        }
        return Result.error().setMessage(result);
    }
}
